package midoo.server.core;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelOption;
import midoo.common.MConfig;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by ajianzheng on 2015/4/10.
 *
 */
public class MServerBootstrapConfig implements MConfig {
    private String serverName = "Kiwi-1.0";
    private int bossThreadCount;
    private int workerThreadCount;
    private String host;
    private int port;

    /// options
    private Map<ChannelOption, Object> options = new HashMap<ChannelOption, Object>();
    private Map<ChannelOption, Object> childOptions = new HashMap<ChannelOption, Object>();

    private MServerBootstrapOption optionUtil;
    private MServerBootstrapOption childOptionUtil;

    /// handlers
    private boolean addLastLoggingHandler = false;
//    private LogLevel logLevel;

    private int maxConnections = 100*1024;

    private boolean addLastIdleStateHandler = false;
    private int readerIdleTimeSeconds = 0;
    private int writerIdleTimeSeconds = 0;
    private int allIdleTimeSeconds = 120;

    private ChannelHandler decoder;
    private ChannelHandler encoder;

    public MServerBootstrapConfig() {
        bossThreadCount = 1;
        workerThreadCount = Runtime.getRuntime().availableProcessors();

        host = "0.0.0.0";
        port = 0;

        /// default options
        options.put(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000);
        options.put(ChannelOption.SO_BACKLOG, 1024);
        options.put(ChannelOption.SO_REUSEADDR, true);
        childOptions.put(ChannelOption.TCP_NODELAY, true);
    }

    public ChannelHandler getDecoder() {
        return decoder;
    }

    public void setDecoder(ChannelHandler decoder) {
        this.decoder = decoder;
    }

    public ChannelHandler getEncoder() {
        return encoder;
    }

    public void setEncoder(ChannelHandler encoder) {
        this.encoder = encoder;
    }

    public boolean isAddLastLoggingHandler() {
        return addLastLoggingHandler;
    }

    public void setAddLastLoggingHandler(boolean addLastLoggingHandler) {
        this.addLastLoggingHandler = addLastLoggingHandler;
    }

//    public LogLevel getLogLevel() {
//        return logLevel;
//    }
//
//    public void setLogLevel(LogLevel logLevel) {
//        this.logLevel = logLevel;
//    }

    public int getMaxConnections() {
        return maxConnections;
    }

    public void setMaxConnections(int maxChannels) {
        this.maxConnections = maxChannels;
    }

    public boolean isAddLastIdleStateHandler() {
        return addLastIdleStateHandler;
    }

    public void setAddLastIdleStateHandler(boolean addLastIdleStateHandler) {
        this.addLastIdleStateHandler = addLastIdleStateHandler;
    }

    public int getReaderIdleTimeSeconds() {
        return readerIdleTimeSeconds;
    }

    public void setReaderIdleTimeSeconds(int readerIdleTimeSeconds) {
        this.readerIdleTimeSeconds = readerIdleTimeSeconds;
    }

    public int getWriterIdleTimeSeconds() {
        return writerIdleTimeSeconds;
    }

    public void setWriterIdleTimeSeconds(int writerIdleTimeSeconds) {
        this.writerIdleTimeSeconds = writerIdleTimeSeconds;
    }

    public int getAllIdleTimeSeconds() {
        return allIdleTimeSeconds;
    }

    public void setAllIdleTimeSeconds(int allIdleTimeSeconds) {
        this.allIdleTimeSeconds = allIdleTimeSeconds;
    }

    public MServerBootstrapOption getOptionUtil() {
        return optionUtil;
    }

    public void setOptionUtil(MServerBootstrapOption optionUtil) {
        this.optionUtil = optionUtil;
        this.options.putAll(optionUtil.getOptions());
    }

    public MServerBootstrapOption getChildOptionUtil() {
        return childOptionUtil;
    }

    public void setChildOptionUtil(MServerBootstrapOption childOptionUtil) {
        this.childOptionUtil = childOptionUtil;
        this.childOptions.putAll(childOptionUtil.getOptions());
    }

    public Map<ChannelOption, Object> getOptions() {
        return options;
    }

    public Map<ChannelOption, Object> getChildOptions() {
        return childOptions;
    }

    public int getBossThreadCount() {
        return bossThreadCount;
    }

    public void setBossThreadCount(int bossThreadCount) {
        this.bossThreadCount = bossThreadCount;
    }

    public int getWorkerThreadCount() {
        return workerThreadCount;
    }

    public void setWorkerThreadCount(int workerThreadCount) {
        this.workerThreadCount = workerThreadCount;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getServerName() {
        return serverName;
    }

    public void setServerName(String serverName) {
        this.serverName = serverName;
    }
}
